package vn.edu.saigontech.ncms.service.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.program.Level;
import vn.edu.saigontech.ncms.model.program.Program;
import vn.edu.saigontech.ncms.service.LevelService;

@SuppressWarnings("unchecked")
public class LevelServiceImpl extends HibernateGenericService<Level, Long> implements LevelService {

	@Override
	public Level findByName(String name) {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq("name", name));
		return (Level) criteria.uniqueResult();
	}

	@Override
	public List<Level> findByProgram(Program program) {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq("program", program));
		return criteria.list();
	}

	@Override
	public Level findNextLevel(Level level) {
		Criteria criteria = this.createCriteria();
		if (level != null) {
			criteria.add(Restrictions.eq("program", level.getProgram()));
			criteria.add(Restrictions.eq("order", level.getOrder() + 1));
			return (Level) criteria.uniqueResult();
		}
		return null;
	}
}